package com.dianrui.moonfire.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.adapter.CouponListAdapter;
import com.dianrui.moonfire.bean.CouponModel;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.JsonUtils;
import com.dianrui.moonfire.util.NoDoubleClickUtils;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:选择优惠劵
 * User: lzy
 * Date: 2020-8-17
 */
public class CouponListActivity extends BaseActivity {
    private List<CouponModel> couponModelList = new ArrayList<>();
    private CouponListAdapter couponListAdapter;
    private String moneys;
    private String couponIds;
    private String isDisable;
    private String mc_id;

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.coupon_list;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                mc_id = bundle.getString("mc_id");
            }

        }
        customInit(true);
        title.setText(getString(R.string.recharge_txt12));
        topRight.setText(getString(R.string.sure));
        topRight.setVisibility(View.VISIBLE);
        getCouponList();
        LinearLayoutManager layoutManager = new LinearLayoutManager(CouponListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        couponListAdapter = new CouponListAdapter(couponModelList);
        recyclerView.setAdapter(couponListAdapter);
        couponListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CouponModel couponModel = couponModelList.get(position);
                if (couponModel != null) {
                    switch (view.getId()) {
                        case R.id.click_coupen_layout:
                            for (int i = 0; i < couponModelList.size(); i++) {
                                if (i == position) {
                                    if (couponModelList.get(i).is_check.equals("1")){
                                        couponModelList.get(i).is_check = "0";
                                        moneys = "";
                                        couponIds = "";
                                        isDisable = "";
                                    }else {
                                        couponModelList.get(i).is_check = "1";
                                        moneys = couponModel.discount;
                                        couponIds = couponModel.mc_id;
                                        isDisable = couponModel.is_disable;
                                    }
                                }else{
                                    couponModelList.get(i).is_check = "0";
                                }
                            }
                            couponListAdapter.setNewData(couponModelList);
                            break;

                        case R.id.coupon_cb_balance:
                            for (int i = 0; i < couponModelList.size(); i++) {
                                if (i == position) {
                                    if (couponModelList.get(i).is_check.equals("1")){
                                        couponModelList.get(i).is_check = "0";
                                        moneys = "";
                                        couponIds = "";
                                        isDisable = "";
                                    }else {
                                        couponModelList.get(i).is_check = "1";
                                        moneys = couponModel.discount;
                                        couponIds = couponModel.mc_id;
                                        isDisable = couponModel.is_disable;
                                    }
                                }else{
                                    couponModelList.get(i).is_check = "0";
                                }
                            }
                            couponListAdapter.setNewData(couponModelList);
                            break;

                        default:
                            break;
                    }
                }
            }
        });
    }

    @Override
    public void initData() {
    }


    private void getCouponList() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        if (!StringUtils.isEmpty(mc_id)) {
            jsonObject.addProperty("mc_id", mc_id);
        }
        try {
            XHttpRequest.getInstance().postRequests(Url.COUPONLIST, jsonObject.toString(), new XHttpRequest.XCallBack() {
                @Override
                public void onResponse(String result) {
                    if (!StringUtils.isEmpty(result)) {
                        try {
                            if (isFinishing()) {
                                return;
                            }
                            String status = JsonUtils.getJsonStr(result, Constant.STATUS);
                            if (Constant.DATASUCCESS.equals(status)) {
                                if (couponModelList != null) {
                                    couponModelList.clear();
                                }
                                String data = JsonUtils.getJsonStr(result, "data");
                                if (!StringUtils.isEmpty(data)) {
                                    List<CouponModel> list = JsonUtils.jsonStrToList(data, new TypeToken<ArrayList<CouponModel>>() {
                                    }.getType());
                                    if (list != null && list.size() > 0) {
                                        couponModelList.addAll(list);
                                        couponListAdapter.setNewData(couponModelList);
                                    }
                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (couponModelList.size() <= 0) {
                            couponListAdapter.getData().clear();
                            couponListAdapter.setEmptyView(LayoutInflater.from(CouponListActivity.this).inflate(R.layout.empty_view, null));
                            couponListAdapter.notifyDataSetChanged();

                        } else {
                            recyclerView.setVisibility(View.VISIBLE);

                        }
                    }
                }

                @Override
                public void failedRequest(String result) {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            });
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.back, R.id.top_right})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right:
                Intent intent = new Intent();
                intent.putExtra("moneys", moneys);
                intent.putExtra("coupon_id", couponIds);
                intent.putExtra("isDisable", isDisable);
                setResult(9, intent);
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (couponModelList != null) {
            couponModelList.clear();
            couponModelList=null;
        }
    }
}
